﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AccidentalNoise
{
    public struct SCache
    {
        public double X, Y, Z, Val;
        public bool Valid;
    }

    public class Cache : ModuleBase
    {
        SCache cache2D;
        SCache cache3D;

        public Cache(ModuleBase source)
        {
            this.Source = source;
        }

        public override double Get(double x, double y)
        {
            if (!cache2D.Valid || cache2D.X != x || cache2D.Y != y)
            {
                cache2D.X = x;
                cache2D.Y = y;
                cache2D.Valid = true;
                cache2D.Val = Source.Get(x, y);
            }

            return cache2D.Val;
        }

        public override double Get(double x, double y, double z)
        {
            if (!cache3D.Valid || cache3D.X != x || cache3D.Y != y || cache3D.Z != z)
            {
                cache3D.X = x;
                cache3D.Y = y;
                cache3D.Z = z;
                cache3D.Valid = true;
                cache3D.Val = Source.Get(x, y, z);
            }

            return cache3D.Val;
        }
    }
}
